package com.gopay.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dynamicode.invoked.Standard;
import com.dynamicode.parsereturncode.ParseReturnCode;
import com.dynamicode.parsereturncode.TokenInfoBean;
import com.dynamicode.parsereturncode.UserInfoBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


/**
 * 动态口令卡的工具类
 * @author qianld
 *
 */
@Component
public class DCUtil {
	private static final Logger logger = LoggerFactory.getLogger(DCUtil.class);
	
	public static Standard standard=null;

	@Value("#{webProperties['dcutil.server.host']}")
	private String serverHost ;

	@Value("#{webProperties['dcutil.server.port']}")
	private String serverPort ;

	private static List<HashMap<String, String>> serverList = new ArrayList<HashMap<String, String>>();

	@PostConstruct
	public void init() {
		HashMap<String, String> server1 = new HashMap<String, String>();
		server1.put("host", serverHost);
		server1.put("port", serverPort);//dcsrv有关的
		server1.put("key", "20080808");
		logger.info("serverHost:{},serverPort:{}",serverHost,serverPort);
		serverList.add(server1);
	}

	static{
		try {
			/*serverList = new ArrayList<HashMap<String, String>>();
			HashMap<String, String> server1 = new HashMap<String, String>();
			server1.put("host", "211.88.2.77");
			server1.put("port", "8888");//dcsrv有关的
			server1.put("key", "20080808");
			serverList.add(server1);*/
			standard = new Standard(serverList);
		} catch (Exception e) {
		logger.error("连接动联系统服务报错了");
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * 根据令牌序列号验证动态密码
	 * @param tokenId
	 * @param dcPass
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map CheckDynamicPwdByToken(String tokenId,String dcPass) throws Exception{
		Map map=new HashMap<>();
		ParseReturnCode returnMsg = standard.CheckDynamicPwdByToken("zh_CN", 1/*引擎编号*/, tokenId/*令牌序列号*/, dcPass);
	    map.put("retCode", returnMsg.getRetcode());
	    map.put("retdesc", returnMsg.getRetdesc());
		return map;
	}
	
	
	/**
	 * 根据令牌序列号启用令牌
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map tokeEnable(String tokenId) throws Exception{
		ParseReturnCode returnMsg = standard.TokeEnable("zh_CN", 1, tokenId);
		Map map=new HashMap<>();
		map.put("retCode", returnMsg.getRetcode());//返回代码，0表示操作成功，非0表示出错。
	    map.put("retdesc", returnMsg.getRetdesc());//返回码描述信息
		return map;
	}
	
	/**
	 * 根据令牌序列号禁用令牌
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map tokenDisable(String tokenId) throws Exception{
		ParseReturnCode returnMsg = standard. TokenDisable ("zh_CN",1,tokenId);
		Map map=new HashMap<>();
		map.put("retCode", returnMsg.getRetcode());//返回代码，0表示操作成功，非0表示出错。
	    map.put("retdesc", returnMsg.getRetdesc());//返回码描述信息
		return map;
	}
	
	
	/**
	 * 根据令牌序列号禁用令牌
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map tokenUnlock(String tokenId) throws Exception{
		ParseReturnCode returnMsg = standard. TokenUnlock ("zh_CN",1, tokenId);
		Map map=new HashMap<>();
		map.put("retCode", returnMsg.getRetcode());//返回代码，0表示操作成功，非0表示出错。
	    map.put("retdesc", returnMsg.getRetdesc());//返回码描述信息
		return map;
	}
	
	/**
	 * 绑定令牌(验证口令密码)
	 * @param tokenId
	 * @param dcPass
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map appBindByDp(String username, String tokenId, String dcPwd) throws Exception{
		Map map=new HashMap<>();
		ParseReturnCode returnMsg = standard.AppBindByDp("zh_CN", 57, username, 1, tokenId, dcPwd);
	    map.put("retCode", returnMsg.getRetcode());
	    map.put("retdesc", returnMsg.getRetdesc());
		return map;
	}
	
	/**
	 * 解绑
	 * @param tokenId
	 * @param dcPass
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map appUnbind(String username, String tokenId) throws Exception{
		Map map=new HashMap<>();
		ParseReturnCode returnMsg = standard.AppUnbind("zh_CN", 57, username, 1, tokenId);
	    map.put("retCode", returnMsg.getRetcode());
	    map.put("retdesc", returnMsg.getRetdesc());
		return map;
	}
	
	/**
	 * 根据用户名查询令牌，因需求只有一对一的情况，故直接返回令牌信息
	 * @throws Exception 
	 */
	public static TokenInfoBean tokenQueryByUser(String username) throws Exception{
		ParseReturnCode tokenQuery = DCUtil.tokenQuery(username, null);
		Object object = (tokenQuery.getTokenList() == null) ? null : tokenQuery.getTokenList().get(0);
		return (TokenInfoBean)object;
	}
	
	/**
	 * 根据令牌序列号查询令牌，因需求只有一对一的情况，故直接返回令牌信息
	 * @throws Exception 
	 */
	public static TokenInfoBean tokenQueryByToken(String tokensn) throws Exception{
		ParseReturnCode tokenQuery = DCUtil.tokenQuery(null, tokensn);
		logger.info("第二层"+tokenQuery.getTokenList());
		Object object = (tokenQuery.getTokenList() == null) ? null : tokenQuery.getTokenList().get(0);
		return (TokenInfoBean)object;
	}
	
	/**
	 * 根据令牌查询用户名，因需求只有一对一的情况，故直接返回用户信息
	 * @throws Exception 
	 */
	public static UserInfoBean userQueryByToken(String tokensn) throws Exception{
		ParseReturnCode tokenQuery = DCUtil.tokenQuery(null, tokensn);
		Object object = (tokenQuery.getUserList() == null) ? null : tokenQuery.getUserList().get(0);
		return (UserInfoBean)object;
	}
	/**
	 * 根据令牌查询用户名，因需求只有一对一的情况，故直接返回用户信息
	 * @throws Exception 
	 */
	public static UserInfoBean userQueryByUser(String username) throws Exception{
		ParseReturnCode tokenQuery = DCUtil.tokenQuery(username, null);
		Object object = (tokenQuery.getUserList() == null) ? null : tokenQuery.getUserList().get(0);
		return (UserInfoBean)object;
	}
	/**
	 * 判断令牌序列号是否存在
	 * @throws Exception 
	 */
	public static boolean tokensnExist(String tokensn) throws Exception{
		TokenInfoBean token = DCUtil.tokenQueryByToken(tokensn);
		logger.info("第一层"+token);
		return token == null ? false : true;
	}
	
	/**
	 * 判断令牌序列号是否已被绑定过, 如果该令牌下由用户，即被绑定
	 * @throws Exception 
	 */
	public static boolean tokensnIsbind(String tokensn) throws Exception{
		UserInfoBean user = DCUtil.userQueryByToken(tokensn);
		return user == null ? false : true;
	}
	
	/**
	 * 根据用户名和令牌序列号查询令牌
	 * @throws Exception 
	 */
	public static ParseReturnCode tokenQuery(String username,String tokensn) throws Exception{
		return standard.tokenQuery ("zh_CN",57, username,1,tokensn);
	}
	
	

}
